package com.mj.factory.workOrder;

import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import com.mj.mes.utils.IsEmpty;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.*;
import java.util.List;

@Service
@Transactional
public class WorkOrderService {

    @Autowired
    WorkOrderDao workOrderDao;

    @Autowired
    @PersistenceContext
    protected EntityManager em;

    Page<WorkOrder> findAll(Pageable pageable) {
        return workOrderDao.findAll(pageable);
    }

    Page<WorkOrder> findAll(Specification spec, Pageable pageable) {
        return workOrderDao.findAll(spec,pageable);
    }

    Page<WorkOrder> findAll(String projectName,
                            String partName,
                            String chartNumber,
                            String statusName,
                            Pageable page) {
        return workOrderDao.findByProductOrder_ProjectNameLikeAndPartNameLikeAndChartNumberLikeAndWorkOrderStatusLikeAndWorkOrderStatusNot
                (projectName,partName,chartNumber,statusName,
                        WorkOrderStatus.StatusEnum.Not_Ready.toString(),page);
    }

    /**
     * 执行SQL语句返回list
     * @return list
     */
    public List<WorkOrder> findWorkOrderBySql(String findMaterialNumber ,String findPartName,String findMaterial,String findMaterialSpec,String findChartNumber,String findArrivalDate,Integer page, Integer rows) {

        StringBuffer sb=new StringBuffer("select * from mj_work_order ");
        if(!IsEmpty.isEmpty(findMaterialNumber)){
            sb.append(" and material_number like '%"+findMaterialNumber+"%' ");
        }
        if(!IsEmpty.isEmpty(findPartName)){
            sb.append(" and part_name like '%"+findPartName+"%' ");
        }
        if(!IsEmpty.isEmpty(findMaterial)){
            sb.append(" and material like '%"+findMaterial+"%' ");
        }
        if(!IsEmpty.isEmpty(findMaterialSpec)){
            sb.append(" and material_spec like '%"+findMaterialSpec+"%'  ");
        }
        if(!IsEmpty.isEmpty(findChartNumber)){
            sb.append(" and chart_number like '%"+findChartNumber+"%' ");
        }

        if(!IsEmpty.isEmpty(findArrivalDate)){
            sb.append(" and arrival_date='"+findArrivalDate+"' ");
        }
        sb.append("  limit "+(page-1)+","+rows);
        Query query = em.createNativeQuery(sb.toString().replaceFirst("and","where"));
        query.unwrap(SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        List list = query.getResultList();
        List<WorkOrder> workOrderList=new LinkedList<WorkOrder>();
        for (Object obj : list) {
            Map row = (Map) obj;
            WorkOrder wo = new WorkOrder();
            wo.setId(Long.valueOf(String.valueOf(row.get("id"))));
            wo.setSerialNumber((String) row.get("serial_number"));
            wo.setPartName((String) row.get("part_name"));
            wo.setMaterial((String) row.get("material"));
            wo.setMaterialNumber((String) row.get("material_number"));
            wo.setArrivalDate((Date) row.get("arrival_date"));
            wo.setChartNumber((String) row.get("chart_number"));
            wo.setMaterialSpec((String) row.get("material_spec"));
            wo.setOderNumber((String) row.get("oder_number"));
            wo.setRemarksFig((String) row.get("remarks_fig"));
            wo.setUnit((String) row.get("unit"));
            workOrderList.add(wo);
        }

        return workOrderList;
    }

    List<WorkOrder> findAll() {
        return workOrderDao.findAll();
    }

    public List<WorkOrder> findByProductionProjectId(long project_id){
//        return projectItemDao.findByProductionProjectIdOrderBySequenceNumber(project_id);
        return null;
    }

    public boolean delete(List<WorkOrder> list){
        boolean flag=false;
        try {
            workOrderDao.deleteInBatch(list);
            flag=true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    public WorkOrder findById(long id){
        return workOrderDao.findOne(id);
    }

    public WorkOrder findByChartNumber(String carNum) {
        return workOrderDao.findByChartNumber(carNum);
    }

    public WorkOrder save(WorkOrder workOrder) {
        return workOrderDao.save(workOrder);
    }

}
